/******************************************************************************
 * @File name   :      DealerInfoSyncServiceImpl.java
 *
 * @Author      :      GUDONG
 *
 * @Date        :      2015年10月14日
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 Capgemini, Inc. All  Rights Reserved.
 * This software is published under the terms of the Capgemini Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2015年10月14日 下午5:29:05        GUDONG     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.dealerinfosync.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cap.bts.crmdp.dealer.dealerinfosync.dao.IDealerInfoDao;
import com.cap.bts.crmdp.dealer.dealerinfosync.dao.IDealerInfoSyncDao;
import com.cap.bts.crmdp.dealer.dealerinfosync.domain.DealerInfoRelMars;
import com.cap.bts.crmdp.dealer.dealerinfosync.service.IDealerInfoSyncService;
import com.cap.bts.crmdp.dealer.masterdata.domain.MasterDataDealerInfo;
import com.cap.bts.framework.common.exception.AppException;

/**
 *
 */
@Transactional(rollbackFor = AppException.class)
@Service
public class DealerInfoSyncServiceImpl implements IDealerInfoSyncService {
	
	private static final Logger LOGGER = Logger.getLogger(DealerInfoSyncServiceImpl.class);
	@Autowired
	IDealerInfoSyncDao dealerInfoSyncDao;
    @Autowired
    IDealerInfoDao dealerInfoDao;
    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年10月14日
     * @see com.cap.bts.crmdp.dealer.dealerinfosync.service.IDealerInfoSyncService#deleteDealerInfoSync()
    **/
    @Override
    public void deleteDealerInfoSync() throws AppException {
        dealerInfoSyncDao.deleteDealerInfoSync();
    }
    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年10月14日
     * @see com.cap.bts.crmdp.dealer.dealerinfosync.service.IDealerInfoSyncService#saveDealerInfoSync(com.cap.bts.crmdp.dealer.dealerinfosync.dto.DealerInfoSyncDto)
    **/
    @Override
    public void saveDealerInfoSync(DealerInfoRelMars dealerInfoRelMars) throws AppException {
        dealerInfoSyncDao.save(dealerInfoRelMars);
    }
    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年10月16日
     * @see com.cap.bts.crmdp.dealer.dealerinfosync.service.IDealerInfoSyncService#saveFile(java.util.List)
    **/
    @Override
    public void saveFile(List<Map<String, Object>> dataList) throws AppException {
        this.deleteDealerInfoSync();
        for(int i=0;i<dataList.size();i++){
            Map<String, Object> raw = dataList.get(i);
            DealerInfoRelMars dealerInfoRelMars = new DealerInfoRelMars();
            dealerInfoRelMars.setCompany(raw.get("company").toString());
            dealerInfoRelMars.setDealerId(Integer.valueOf(raw.get("dealerId").toString()));
            dealerInfoRelMars.setMarsDealerId(Integer.valueOf(raw.get("dealerId").toString()));
            dealerInfoRelMars.setMarsTenantId(raw.get("tenantID").toString());
            this.saveDealerInfoSync(dealerInfoRelMars);
        }
    }
    
    
	@Override
	public DealerInfoRelMars getMarsTenantByDearId(Integer dealerId) {
		String hql = "from DealerInfoRelMars where dealerId = :dealerId";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dealerId", dealerId);
		List<DealerInfoRelMars> list = dealerInfoSyncDao.getListByQueryWithMap(hql, paramMap);
		if(list != null && !list.isEmpty()){
			return list.get(0);
		}
		return null;
	}
	@Override
	public MasterDataDealerInfo getDealerInfoByDealerNum(String dealerNum) {
		try {
			List<MasterDataDealerInfo> list = dealerInfoDao.getDealerInfoByDealerNum(dealerNum);
			if(list != null && !list.isEmpty()){
				return list.get(0);
			}
		} catch (AppException e) {
			LOGGER.error("ERROR getDealerInfoByDealerNum");
		}
		return null;
	}

}
